package com.godme.code.endpoint;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.godme.code.sentinel.CodeSenderSentinelResource;
import com.godme.code.service.CodeService;
import com.godme.service.code.CodeManager;
import com.godme.service.mail.CodeSender;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;

@DubboService(interfaceClass = CodeManager.class)
class CodeManagerImpl implements CodeManager {

    @Resource
    private CodeService codeService;

    @DubboReference(check = false)
    private CodeSender codeSender;


    @SentinelResource(value = "sendCode", fallback = "sendCodeFallback", blockHandler = "sendCodeBlock", fallbackClass = CodeSenderSentinelResource.class, blockHandlerClass = CodeSenderSentinelResource.class)
    public boolean sendCode(String mail){
        return codeSender.sendCode(mail, codeService.generate(mail));
    }


    @SentinelResource(value = "validCode", fallback = "validCodeFallback", blockHandler = "validCodeBlock", fallbackClass = CodeSenderSentinelResource.class, blockHandlerClass = CodeSenderSentinelResource.class)
    public int validCode(String mail, String code){
        return codeService.validCode(mail, code);
    }
}
